package com.project.action;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionContext;
import com.project.entity.Announcement;
import com.project.entity.GeneralManager;
import com.project.entity.News;
import com.project.entity.ProjectManagement;
import com.project.entity.SuperAdministrator;
import com.project.service.AnnouncementManager;
import com.project.service.DataManage;
import com.project.service.NewsManage;
import com.project.service.SystemManage;
import com.project.entity.User;

public class SystemAction {
	private		String	pageMessage1,pageMessage2,start,limit;
	private		String	jsonString;
	
	private		SuperAdministrator		administrator;
	private		GeneralManager			manager;
	private		SystemManage			systemManageService;
	private		DataManage				dataMangeService;
	private		NewsManage				NewsManageService;
	private 	AnnouncementManager		announcementManager;
	private		final String pageSize = "25"; //翻页的size
	
	public void clearValue(){
		pageMessage1 = null;
		pageMessage2 = null;
		start = null;
		limit = null;
	}

	/**
	 * 超级管理员登陆
	 * @return
	 */
	public	String	superLogin(){
		HttpServletRequest request = ServletActionContext.getRequest(); 
		ActionContext context = ActionContext.getContext();	
		if (pageMessage1 == null || pageMessage1.equals("")
				|| pageMessage2 == null || pageMessage2.equals("")){
			context.getSession().put("failMessageBoolean", 1);
			return "fail";
		}
		administrator = systemManageService.getSuper(pageMessage1);
		if ( administrator == null || !pageMessage2.equals(administrator.getSuperadministratorpassword())){
			context.getSession().put("failMessageBoolean", 1);
			return "fail";
		}
		administrator.setSuperadministratorpassword("");
		JSONArray jsonObject = JSONArray.fromObject(administrator);
		context.getSession().clear();
		
		context.getSession().put("login", "yes");
		context.getSession().put("admin", administrator);
		context.getSession().put("name", administrator.getSuperadministratorname());
		context.getSession().put("jobNumber", administrator.getJobnumber());
		context.getSession().put("id", administrator.getSuperadministratorid());
		context.getSession().put("right", "super");
		context.getSession().put("failMessageBoolean", 0);
		this.setJsonString("{success:true,totalCount :1 , list:" + jsonObject.toString() + "}");
		Map<String,String> map = new HashMap<String,String>();
		map.put("start", String.valueOf(0));
		map.put("limit", String.valueOf(20));
		List<News> newslist = this.NewsManageService.getAllNews(map); //获取新闻
		context.getSession().put("newslist", newslist);
		
		List<Announcement> noticelist = this.announcementManager.getAllNotice(map); //获取通知
		context.getSession().put("noticelist", noticelist);
//		request.setAttribute("newslist", newslist);
		clearValue();
	    return "success";
	}
	
	/**
	 * 普通管理员登陆
	 * @return
	 */
	public	String	managerLogin(){
		
		ActionContext context = ActionContext.getContext();	
		if (pageMessage1 == null || pageMessage1.equals("")
				|| pageMessage2 == null || pageMessage2.equals("")){
			context.getSession().put("failMessageBoolean", 1);
			return "fail";
		}
		manager = systemManageService.getManager(pageMessage1);
		if(manager == null)
			manager = systemManageService.getManagerByNumber(pageMessage1);
		if ( manager == null || !pageMessage2.equals(manager.getGeneralmanagerpassword())){
			context.getSession().put("failMessageBoolean", 1);
			return "fail";
		}
		manager.setGeneralmanagerpassword("");
		JSONArray jsonObject = JSONArray.fromObject(manager);
		context.getSession().clear();
		
		context.getSession().put("login", "yes");
		context.getSession().put("manager", manager);
		context.getSession().put("id", manager.getGeneralmanagerid());
		context.getSession().put("name", manager.getGeneralmanagername());
		context.getSession().put("jobNumber", manager.getJobnumber());
		context.getSession().put("right", "manager");
		context.getSession().put("failMessageBoolean", 0);
		this.setJsonString("{success:true,totalCount :1 , list:" + jsonObject.toString() + "}");
		Map<String,String> map = new HashMap<String,String>();
		map.put("start", String.valueOf(0));
		map.put("limit", String.valueOf(20));
		List<News> newslist = this.NewsManageService.getAllNews(map);
		context.getSession().put("newslist", newslist);
		List<Announcement> noticelist = this.announcementManager.getAllNotice(map);
		context.getSession().put("noticelist", noticelist);
		
		clearValue();
	    return "success";
	}
	
	public String	userLogin(){
		ActionContext context = ActionContext.getContext();	
		if (pageMessage1 == null || pageMessage1.equals("")
				|| pageMessage2 == null || pageMessage2.equals("")){
			context.getSession().put("failMessageBoolean", 1);
			return "fail";
		}
		context.getSession().clear();
		User user = this.systemManageService.getUser(pageMessage1);
		if ( user == null || !pageMessage2.equals(user.getUserpassword())){
			context.getSession().put("failMessageBoolean", 1);
			return "fail";
		}
		ProjectManagement	project	 = this.dataMangeService.getByNumber(pageMessage1);
		JSONArray jsonObject = JSONArray.fromObject(user);
		
		context.getSession().put("login", "yes");
		context.getSession().put("project", project);
		context.getSession().put("user", user);
		context.getSession().put("id", user.getUserid());
		context.getSession().put("name", user.getUsername());
		context.getSession().put("number", user.getProjectnumber());
		context.getSession().put("id", user.getUserid());
		context.getSession().put("right", "user");
		context.getSession().put("failMessageBoolean", 0);
		this.setJsonString("{success:true,totalCount :1 , list:" + jsonObject.toString() + "}");
		
		
		Map<String,String> map = new HashMap<String,String>();
		map.put("start", String.valueOf(0));
		map.put("limit", String.valueOf(20));
		List<News> newslist = this.NewsManageService.getAllNews(map);
		context.getSession().put("newslist", newslist);
		List<Announcement> noticelist = this.announcementManager.getAllNotice(map);
		context.getSession().put("noticelist", noticelist);
		clearValue();
	    return "success";
	}
	
	public String logout(){
		ActionContext context = ActionContext.getContext();	
		context.getSession().clear();
		context.getSession().put("login", "no");
		return "success";
	}
	
	
	public String getNewsDetail(){
		HttpServletRequest request = ServletActionContext.getRequest(); 
		News news = this.NewsManageService.getNewsById(Integer.valueOf(pageMessage1));
		request.setAttribute("news", news);
		clearValue();
		return "success";
	}
	
	public String getNotice() throws UnsupportedEncodingException{
		HttpServletRequest request = ServletActionContext.getRequest(); 
		//HttpServletResponse response = ServletActionContext.getResponse();
		Announcement notice = this.announcementManager.findById(Integer.valueOf(pageMessage1));
		request.setAttribute("notice", notice);
		request.setAttribute("fileName",notice.getAnnouncementattachment());
		//response.setHeader("Content-Disposition", "attachment;" +notice.getAnnouncementattachment()+"=" + java.net.URLEncoder.encode(notice.getAnnouncementattachment(), "UTF-8")); 
		clearValue();
		return "success";
	}
	
	public String start(){
		ActionContext context = ActionContext.getContext();	
		
		Map<String,String> map = new HashMap<String,String>();
		map.put("start", String.valueOf(0));
		map.put("limit", String.valueOf(20));
		List<News> newslist = this.NewsManageService.getAllNews(map);
		context.getSession().put("newslist", newslist);
		List<Announcement> noticelist = this.announcementManager.getAllNotice(map);
		context.getSession().put("noticelist", noticelist);
		return "success";
	}

	/**
	 * 转到超级管理员的系统管理页面
	 * @return
	 */
	public String adminpage(){
		ActionContext context = ActionContext.getContext();	
		if(context.getSession().get("right")!=null){
			if(context.getSession().get("right").equals("super"))
				return "success";
		}
		return "fail";
	}
	
	
	/**
	 * 转到用户的系统管理页面
	 * @return
	 */
	public String userpage(){
		ActionContext context = ActionContext.getContext();	
		if(context.getSession().get("right")!=null){
			if(context.getSession().get("right").equals("user"))
				return "success";
		}
		return "fail";
	}
	
	/**
	 * 转到管理员的页面
	 * @return
	 */
	public String managerpage(){
		ActionContext context = ActionContext.getContext();	
		if(context.getSession().get("right")!=null){
			if(context.getSession().get("right").equals("manager"))
				return "success";
		}
		return "fail";
	}
	
	/**
	 * 通过分页获取所有的新闻列表
	 * @return
	 */
	public String getNewsList(){
		HttpServletRequest request = ServletActionContext.getRequest(); 
		ActionContext context = ActionContext.getContext();	
		int newsTotalCount;
		newsTotalCount = this.NewsManageService.totalCount(null);
		int pageCount = newsTotalCount/Integer.valueOf(this.pageSize);
		if(start== null  || start.equals(""))
			return "fail";
		Map<String,String> map = new HashMap<String,String>();
		map.put("start", start);
		map.put("limit", pageSize);
		List<News> newslist = this.NewsManageService.getAllNews(map);
		
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("page", Integer.valueOf(start));
		request.setAttribute("newslist", newslist);
		this.clearValue();
		return "success";
	}
	
	/**
	 * 通过分页获取所有的通知列表
	 * @return
	 */
	public String getNoticeList(){
		HttpServletRequest request = ServletActionContext.getRequest(); 
		ActionContext context = ActionContext.getContext();	
		int NoticeTotalCount;
		NoticeTotalCount = this.announcementManager.totalCount(null);
		int pageCount = NoticeTotalCount/Integer.valueOf(this.pageSize);
		if(start== null  || start.equals(""))
			return "fail";
		Map<String,String> map = new HashMap<String,String>();
		map.put("start", start);
		map.put("limit", this.pageSize);
		List<Announcement> noticelist = this.announcementManager.getAllNotice(map);
		
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("page", Integer.valueOf(start));
		request.setAttribute("noticelist", noticelist);
		this.clearValue();
		return "success";
	}
	
	
	public String getPageMessage1() {
		return pageMessage1;
	}

	public void setPageMessage1(String pageMessage1) {
		this.pageMessage1 = pageMessage1;
	}

	public String getPageMessage2() {
		return pageMessage2;
	}

	public void setPageMessage2(String pageMessage2) {
		this.pageMessage2 = pageMessage2;
	}

	public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public SuperAdministrator getAdministrator() {
		return administrator;
	}

	public void setAdministrator(SuperAdministrator administrator) {
		this.administrator = administrator;
	}

	public GeneralManager getManager() {
		return manager;
	}
	public void setManager(GeneralManager manager) {
		this.manager = manager;
	}

	public SystemManage getSystemManageService() {
		return systemManageService;
	}

	public void setSystemManageService(SystemManage systemManageService) {
		this.systemManageService = systemManageService;
	}
	public String getLimit() {
		return limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}


	public AnnouncementManager getAnnouncementManager() {
		return announcementManager;
	}

	public void setAnnouncementManager(AnnouncementManager announcementManager) {
		this.announcementManager = announcementManager;
	}

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	public DataManage getDataMangeService() {
		return dataMangeService;
	}

	public void setDataMangeService(DataManage dataMangeService) {
		this.dataMangeService = dataMangeService;
	}

	public NewsManage getNewsManageService() {
		return NewsManageService;
	}

	public void setNewsManageService(NewsManage newsManageService) {
		NewsManageService = newsManageService;
	}
	
}
